package cn.yoyo.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Document("t_user")
public class User implements UserDetails {
    @Id
    private String id;
    private String nickname;// 昵称
    private String username;// 账户
    private String password;// 密码
    private String role = "C";// 身份
    private String phone;// 电话号码
    private String pictureId;// 头像id
    private Integer closed = 0;// 冻结 0:正常 1:异常-冻结

    public User() {
    }

    public User(String id, String nickname, String username, String password, String role, String phone, String pictureId, Integer closed) {
        this.id = id;
        this.nickname = nickname;
        this.username = username;
        this.password = password;
        this.role = role;
        this.phone = phone;
        this.pictureId = pictureId;
        this.closed = closed;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPictureId() {
        return pictureId;
    }

    public void setPictureId(String pictureId) {
        this.pictureId = pictureId;
    }

    public Integer getClosed() {
        return closed;
    }

    public void setClosed(Integer closed) {
        this.closed = closed;
    }

    @Override// 表示获取登录用户所有权限
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> lst = new ArrayList<>();
        // this.role = "G,P"
        if (this.role==null)return null;
        String arr[] = this.role.split(",");
        for (int i = 0;i<arr.length;i++){
            SimpleGrantedAuthority simple = new SimpleGrantedAuthority(arr[i]);
            lst.add(simple);
        }
        return lst;
    }

    @Override// 表示获取密码
    public String getPassword() {
        return this.password;
    }

    @Override// 表示获取用户名
    public String getUsername() {
        return this.username;
    }

    @Override// 表示判断账户是否过期
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override// 表示判断账户是否被锁定
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override// 表示凭证{密码}是否过期
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override// 表示当前用户是否可用
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", nickname='" + nickname + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role='" + role + '\'' +
                ", phone='" + phone + '\'' +
                ", pictureId='" + pictureId + '\'' +
                ", closed=" + closed +
                '}';
    }
}
